package com.erp.erp_ui.UserGroupMember;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.User.UserEntityExample;
import com.erp.erp_entitys.User.UserEntityVO;
import com.erp.erp_entitys.UserGroupMember.UserGroupMemberEntity;
import com.erp.erp_entitys.UserGroupMember.UserGroupMemberEntityExample;
import com.erp.erp_servers.Department.IDepartmentService;
import com.erp.erp_servers.User.IUserService;
import com.erp.erp_servers.UserGroupMember.IUserGroupMemberService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserGroupMemberControl
 * @Description 用户组控制类
 * @Author songjizhen
 * @Date 2020-07-04 9:44
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "usergroupmember")
 public class UserGroupMemberControl extends Base {
    @Autowired
    IUserGroupMemberService userGroupMemberService;
    @Autowired
    IUserService userService;
    @Autowired
    IDepartmentService departmentService;
    /**
     * 根据主键删除用户组成员
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> delete(Integer id) {
        try {
            int num = userGroupMemberService.deleteByPrimaryKey(id);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "deleteByIds")
    @ResponseBody
    public R<Integer> deleteByIds(String gcode,String[] codes){
        try {
            List<String> list=new ArrayList<String>();
            for(int i=0;i<codes.length;i++){
                list.add(codes[i]);
            }
            UserGroupMemberEntityExample example=new UserGroupMemberEntityExample();
            UserGroupMemberEntityExample.Criteria criteria = example.createCriteria();
            criteria.andUserCodeIn(list);
            criteria.andGroupCodeEqualTo(gcode);
            int num = userGroupMemberService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增用户组成员
     * @param
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(String groupCode,String[] ucodes) {//@RequestBody UserGroupMemberEntity record
        try {
            UserGroupMemberEntity record=new UserGroupMemberEntity();
            int num=0;
            for(int i=0;i<ucodes.length;i++){
                record.setGroupCode(groupCode);
                record.setUserCode(ucodes[i].replace("[","").replace("]","").replace("\"",""));
                num = userGroupMemberService.insertSelective(record);
            }
           // int num = userGroupMemberService.insertSelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据条件查询用户组成员
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
     public R<List<UserGroupMemberEntity>> selectByPage(String gcode) {//@RequestBody UserGroupMemberEntity record,String pageNum,String pageSize
       try {
           if(gcode==null){
               List<UserGroupMemberEntity> list=new ArrayList<UserGroupMemberEntity>();
               return R.build(list);
           }else{
               UserGroupMemberEntity record=new UserGroupMemberEntity();
               record.setGroupCode(gcode);
               List<UserGroupMemberEntity> userGroupMemberEntities = userGroupMemberService.selectByExample(record);
               return R.build(userGroupMemberEntities);
           }

       }catch (Exception e){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
   }

    /**
     * 获取组内成员信息，包括部门
     * @param gcode
     * @return
     */
    @RequestMapping(value = "selectUserByGroup")
    @ResponseBody
    public  List<UserEntityVO> selectUserByGroup(String gcode){
            return null;
    }
    /**
     * 分页获取组内用户信息
     * @param gcode
     * @return
     */
    @RequestMapping(value = "selectUserByGroupByPage")
    @ResponseBody
   public R<List<UserEntity>> selectUserByGroupByPage(String gcode){
       try {
           return R.build(userService.selectUserByGroupCode(gcode));
           /*if(gcode.equals("") ){
               List<UserEntity> list=new ArrayList<UserEntity>();
               return R.build(list);
           }else{
               //获取组内成员code
               UserGroupMemberEntity record=new UserGroupMemberEntity();
               record.setGroupCode(gcode);
               List<UserGroupMemberEntity> userGroupMemberEntities = userGroupMemberService.selectByExample(record);
               List<String> listusercode=new ArrayList<String>();
               for(int i=0;i<userGroupMemberEntities.size();i++){
                   listusercode.add(userGroupMemberEntities.get(i).getUserCode());
               }
               //获取组内成员信息
               UserEntityExample example=new UserEntityExample();
               UserEntityExample.Criteria criteria = example.createCriteria();
               criteria.andCodeIn(listusercode);
               List<UserEntity> userEntities = userService.selectByExample(example);
               //获取组内成员所属部门
               List<String> listDcode=new ArrayList<String>();
               for(int i=0;i<userEntities.size();i++){
                   listDcode.add(userEntities.get(i).getDepartmentCode());
               }
               DepartmentExample dexample=new DepartmentExample();
               DepartmentExample.Criteria criteria1 = dexample.createCriteria();
               criteria1.andCodeIn(listDcode);
               List<DepartmentEntity> departmentEntities = departmentService.selectByExample(dexample);
               Map<String,String> map=new HashMap<String,String>();
               for(int i=0;i<departmentEntities.size();i++){
                   map.put(departmentEntities.get(i).getCode(),departmentEntities.get(i).getName());
               }
               for(int i=0;i<userEntities.size();i++){
                   userEntities.get(i).setDepartmentCode(map.get( userEntities.get(i).getDepartmentCode()));
               }
               return R.build(userEntities);*/


       }catch (Exception e){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
   }
    /**
     * 获取组内用户信息
     * @param gcode
     * @return
     */
    @RequestMapping(value = "selectUserByGroupCode")
    @ResponseBody
    public R<List<UserEntity>> selectUserByGroupCode(String gcode){
        try {
            if(gcode.equals("") ){
                List<UserEntity> list=new ArrayList<UserEntity>();
                return R.build(list);
            }else{
                //获取组内成员code
                UserGroupMemberEntity record=new UserGroupMemberEntity();
                record.setGroupCode(gcode);
                List<UserGroupMemberEntity> userGroupMemberEntities = userGroupMemberService.selectByExample(record);
                List<String> listusercode=new ArrayList<String>();
                for(int i=0;i<userGroupMemberEntities.size();i++){
                    listusercode.add(userGroupMemberEntities.get(i).getUserCode());
                }
                //获取组内成员信息
                UserEntityExample example=new UserEntityExample();
                UserEntityExample.Criteria criteria = example.createCriteria();
                criteria.andCodeIn(listusercode);
                List<UserEntity> userEntities = userService.selectByExample(example);
                return R.build(userEntities);
            }

        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
